import { createRouter, createWebHistory } from 'vue-router'
import Manager from '../views/manager/manager.vue'
import Login from '../views/Login/login.vue'
import Home from '../views/manager/Home/index.vue'


//管理页面的商品
const Items =() =>import('../views/manager/Item/index.vue')
const ItemList =() =>import('../views/manager/Item/itemList.vue')
//管理页面的用户
const User =() =>import('../views/manager/User/index.vue')
const NormalUser = () =>import('../views/manager/User/NormalUser.vue')
const Merchant = () =>import('../views/manager/User/Merchant.vue')
//管理页面的订单
const Order =() =>import('../views/manager/Order/index.vue')
const OrderList =() =>import('../views/manager/Order/OrderList.vue')
//管理页面的字典
const Dictionaries =() =>import('../views/manager/Dictionaries/index.vue')
const DictionaryList =() =>import('../views/manager/Dictionaries/DictionaryList.vue')

const staticRouter = [
  {
    path:'/login',
    component:Login,
    children:[

    ]
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: staticRouter

})

router.beforeEach((to) => {

  staticRouter.forEach(router =>{
    if(router.path == to.path){//静态路由跳过
      return
    }
  })

  //如果该路由存在则不创建
  if (router.getRoutes().some(route => route.path == to.path)){
    return
  }
  //动态路由
  const  menus=JSON.parse(sessionStorage.getItem('menu'))
  menus.forEach((parent) =>{
    const parenRoute = {
      path:parent.path,
      name:parent.name,
      component:() => import(/* @vite-ignore */parent.component),
      children:[]
    }
    parent.children.forEach((children) =>{
      // if (children.name === "文件管理") {
      //               return; // 跳过当前迭代，不处理这个子项
      // }

      const childrenRoute={
        path: children.path,
        name: children.name,
        component: () => import(/* @vite-ignore */children.component),
      }
      parenRoute.children.push(childrenRoute)
    })
    //添加一级路由
    router.addRoute(parenRoute)
  })
  router.push(to.path)
})

export default router
